package cn.tecnova.cd

import cn.tecnova.utils.ConfigHandler
import org.apache.log4j.{Level, Logger}
import org.apache.spark.sql.{Dataset, SQLContext}
import org.apache.spark.{SparkConf, SparkContext}
import org.elasticsearch.spark.rdd.EsSpark

/**
  * description:
  * 47 5463
  **/
object TecnovaPenalty {
  Logger.getLogger("org").setLevel(Level.ERROR)

  def main(args: Array[String]): Unit = {

    val conf = new SparkConf()
      .setAppName(this.getClass.getSimpleName)
      //      .setMaster("local[*]")
      .set("es.index.auto.create", "true")
      .set("es.nodes", ConfigHandler.esNodes)
      .set("es.port", ConfigHandler.esPort)
      .set("es.nodes.wan.only", "true")

    val sc = new SparkContext(conf)
    val sqLContext = new SQLContext(sc)

    import sqLContext.implicits._
    val rowDF = sqLContext.read.jdbc("jdbc:mysql://www.slave4.com:3306/tec_penalty", "qichacha_penalty", ConfigHandler.props2)

    //导入写es相关的包
    import org.elasticsearch.spark.sql._

    val result: Dataset[TecnovaPenalty] = rowDF.map(row => {
      TecnovaPenalty(
        row.getAs[Int]("id"),
        row.getAs[String]("KeyNo"),
        row.getAs[String]("No"),
        row.getAs[String]("DocNo"),
        row.getAs[String]("PenaltyType"),
        row.getAs[String]("OfficeName"),
        row.getAs[String]("Content"),
        row.getAs[String]("PublicDate"),
        row.getAs[String]("PenaltyDate"),
        row.getAs[String]("Remark"),
        row.getAs[String]("PersonName"),
        row.getAs[String]("RegNo"),
        row.getAs[String]("Operator"),
        row.getAs[String]("AdPenaltyDoc"),
        row.getAs[String]("AdPenaltyLink")

      )
    })
//    result.saveToEs("tecnova_penalty2" + "/company_penalty")

    EsSpark.saveToEs(result.rdd,"tecnova_penalty/company_penalty",Map("es.mapping.id" -> "id"))


    sc.stop()

  }

}

case class TecnovaPenalty(

                          var id:Long,
                          var keyNo:String,
                          var no:String,
                          var docNo:String,
                          var penaltyType:String,
                          var officeName:String,
                          var content:String,
                          var publicDate:String,
                          var penaltyDate:String,
                          var remark:String,
                          var personName:String,
                          var regNo:String,
                          var operator:String,
                          var adPenaltyDoc:String,
                          var adPenaltyLink:String

                         )
